import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";
import { appConfig } from "../../appConfig/index";
import NProgress from "nprogress"; // progress bar

const routes: IStbOtype[] = [
  {
    path: "/",
    name: "home",
    meta: {
      title: "后台管理系统"
    },
    component: () => import("../views/Home.vue"),
    children: [
      {
        path: "/system",
        name: "system",
        meta: {
          title: "系统管理",
          menu: true
        },
        children: [
          {
            path: "/user",
            name: "user",
            meta: {
              order: 1,
              menu: true,
              title: "用户管理",
              img: "article.svg"
            },
            component: () => import("../views/user/user.vue")
          },
          {
            path: "/role",
            name: "role",
            meta: {
              order: 2,
              menu: true,
              title: "角色管理",
              img: "article.svg"
            },
            component: () => import("../views/role/role.vue")
          },
          {
            path: "/menu",
            name: "menu",
            meta: {
              order: 3,
              menu: true,
              title: "菜单管理",
              img: "article.svg"
            },
            component: () => import("../views/menu/menu.vue")
          },
        ]
      },
      // {
      //   path: "/example",
      //   name: "example",
      //   meta: {
      //     order: 30,
      //     menu: true,
      //     title: "测试页面",
      //     img: "article.svg"
      //   },
      //   component: () => import("../views/example/example.vue")
      // },
      // {
      //   path: "/coderCreate",
      //   name: "coderCreate",
      //   meta: {
      //     menu: true,
      //     title: "代码生成",
      //     img: "coderCreate.svg"
      //   },
      //   component: () => import("../views/coderCreate/index.vue")
      // }
    ]
  },
  // {
  //   path: "/lockscreen",
  //   name: "lockscreen",
  //   meta: {
  //     title: "锁定屏幕"
  //   },
  //   component: () => import("../views/lockscreen/index.vue")
  // },
  {
    path: "/:pathMatch(.*)*",
    name: "notFound",
    meta: {
      title: "找不到网页"
    },
    component: () => import("../views/notFound/index.vue")
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: "登录"
    },
    component: () => import("../views/login/Login.vue")
  }
];
const router = createRouter({
  history: createWebHashHistory(appConfig[import.meta.env.MODE as IEnvOType].ROUTE_HISTORY),
  routes: routes as RouteRecordRaw[]
});
router.beforeEach(async (to, from) => {
  to.meta.fromName = from.name;
  document.title = `${to.meta.title || ""}`;
  NProgress.start();
  const store = useAllStore();
  if (to.path === "/login") {
    NProgress.done();
    return true;
  } else {
    NProgress.done();
    return store.token ? true : "/login";
  }
});
export default router;
